/**
 * 数据字典类型面板
 */
Ext.namespace("Ext.ux.syscode");

Ext.ux.syscode.TypePanel = Ext.extend(Ext.Panel, {

   	region: 'west',
   	width: 550,
 	minSize: 550,
    maxSize: 650,
    split: true,
	layout: 'border',
	border: false,
	
	selTypeCode: '',	//当前选中的类型编号
	
	formPanel: null,
    grid: null,
    
    
	// 组件事件初始化
	initComponent: function(){
		Ext.ux.syscode.TypePanel.superclass.initComponent.call(this);

		// 定义条件查询面板
		var formPanel = new Ext.FormPanel({
			region: 'north',
			title: '数据字典类型查询',
			frame: true,
			height: 65,
			labelWidth: 70,	// label的长度
			labelAlign: 'right',
			items: [{
				layout: 'column',
				items:[{
					columnWidth: .3,
					layout: 'form',
					items: [{
						xtype: 'textfield',
						fieldLabel: '类型编号',
						name: 'typeCode',
						anchor: '95%'
					}]
				},{
					columnWidth: .3,
					layout: 'form',
					items: [{
						xtype: 'textfield',
						fieldLabel: '类型名称',
						name: 'typeName',
						anchor: '95%'
					}]
				},{
					columnWidth: .4,
					layout: 'hbox',
					defaults:{margins:'0 5 0 0'},
					items: [{
						xtype: 'button',
						text: '查询',
						handler: this.search,
						scope: this
					},{
						xtype: 'button',
						text: '清空',
						handler: this.clearSearchForm,
						scope: this
					}]
				}]
			}]
	    });
		this.add(formPanel);
	    this.formPanel = formPanel;
	    
	    
		
		// 定义业务字典项的列模型
		var gridStructure = [{ 
			name: 'typeId',
			hidden: true
		},{
			name: 'typeCode',  
			header: "类型编号",
			sortable: true,
			editor: new Ext.form.TextField({
				allowBlank: false,
				maxLength: 20
			}),
			width:25
		},{
			name: 'typeName',  
			header: "类型名称",
			sortable: true,
			editor: new Ext.form.TextField({
				allowBlank: false,
				maxLength: 20
			}),
			width:25
		},{
			name: 'typeGrade',  
			header: "类型级别",
			sortable: true,
			editor: new Ext.form.TextField({
				allowBlank: false,
				maxLength: 20
			}),
			width: 10
		},{
			name: 'typeMemo',  
			header: "备注",
			sortable: true,
			editor: new Ext.form.TextField({
				allowBlank: false,
				maxLength: 20
			}),
			width:20
		}];
		
		// 定义业务字典类型表格对象
		var typeGrid = new Ext.ux.grid.RowEditorGrid({
	 		title: '数据字典类型列表',
	 		region: 'center',
	 		margins: '2 0 0 0',
	 		structure: gridStructure,
			defaultSortField: 'typeCode',
			idField: 'typeId',
			readUrl: 'sys-code!typeGridList.action',
			createUrl: 'sys-code!typeGridCreate.action',
			updateUrl: 'sys-code!typeGridUpdate.action',
			destroyUrl: 'sys-code!typeGridDelete.action',
	 		listeners: {
	            rowclick: function(grid, index, ev) {
					var _record = grid.store.getAt(index);
					var _typeCode = _record.get('typeCode');

					if(_typeCode != null && _typeCode != this.selTypeCode){
						this.selTypeCode = _typeCode;
						this.getItemPanel().reload(_typeCode);
					}
				},
				scope: this
	        }
	 	});
	 	
	 	//删除后清空子项面板列表数据
	 	typeGrid.on('deleteRecord', function(){
			this.getItemPanel().clearData();
		}, this);
		
		//保存后清空当前选中类型编号
	 	typeGrid.on('saveRecord', function(grid){
			this.selTypeCode='';
		}, this);
	 	
		this.add(typeGrid);
	    this.grid = typeGrid;
	    
	    // 执行查询操作前传入条件查询参数
		typeGrid.store.on('beforeload', function(){
			typeGrid.store.baseParams = {
				typeCode: formPanel.form.findField('typeCode').getValue(),
				typeName: formPanel.form.findField('typeName').getValue()
			};
		}, this);
		
	},
	
	// 条件查询
	search: function(){
		this.grid.store.reload();
	},
	    
	// 清空
	clearSearchForm: function(){
		this.formPanel.form.reset();
		this.formPanel.form.findField('typeCode').focus();
		this.grid.store.reload();
	},
	
	// 获取子项面板
	getItemPanel: function(){
		return Ext.getCmp('itemPanel');
	}
});
	
